import os
import sys
from Sqlite3Helper import Sqlite3Helper

#filters = "MESSAGE_TESTCASE_FAILED:caseid"

def dealFile( filePath , filters):
    file = open(filePath)
    for line in file:
        for filter in filters:
            if filter in line:
                print filePath
                print line


def getFilesInDir( dir , deep = False):
    fileList = [];
    dirList = [dir];
    while len(dirList) > 0:
        fileDir = dirList.pop()
        files = os.listdir(fileDir)
        for f in files:
            if f.startswith("."):
                continue
            if os.path.isfile(fileDir + "/" + f):
                fileList.append(fileDir + "/" + f)
            elif deep & os.path.isdir(fileDir + "/" + f):
        #        print "dir " + f
                dirList.append(fileDir + "/" + f)
            else :
                pass
                print "else > " + f
    return fileList


sql = Sqlite3Helper()
keywords = sql.queryKeywords()

file = sys.argv[1]
if os.path.isfile(file):
    dealFile(file,keywords)
elif os.path.isdir(file):
    files = getFilesInDir(file,True)
    print "begin dealFile"
    for f in files:
        dealFile(f,keywords)